package com.dmatek.uwb.packet.ianalysis.handler;

import java.net.InetSocketAddress;

import com.dmatek.uwb.packet.bean.AnchorType;
import com.dmatek.uwb.packet.ianalysis.callback.IReportCallBack;
import com.dmatek.uwb.util.tool.AnalyticalUtil;

/**   
 * 播放音频的应答包处理器
 * @author zhangfu  
 * @date 2019年12月3日 下午5:26:27 
 * @version V1.0   
 */
public class PlayVoiceResponseHandler extends BaseAnalysisPackHandler {
	private IReportCallBack<AnchorType> iReportCallBack;
	public PlayVoiceResponseHandler(IReportCallBack<AnchorType> iReportCallBack) {
		super();
		this.iReportCallBack = iReportCallBack;
	}
	// 播放音频成功的回调函数
	@Override
	public boolean templateMethod(byte[] temps, int limit,
			InetSocketAddress sendAddress, int index) throws Exception {
		short cs = 0;
		String strAnchorId = "";
		if (temps[index + 1] == AnalyticalUtil.PLAYVOICE_RESPONSE_TYPE
		 && temps[index + AnalyticalUtil.PLAYVOICE_RESPONSE_SIZE - 1] == AnalyticalUtil.PLAYVOICE_RESPONSE_END) {
			for (int i = index, size = index + AnalyticalUtil.PLAYVOICE_RESPONSE_SIZE - 2; i < size; i++) {
				cs += temps[i];
			}
			cs &= 0x00FF;
			if ((byte) cs == temps[index + AnalyticalUtil.PLAYVOICE_RESPONSE_SIZE - 2]) {
				// 校验相同
				strAnchorId = String.format("%02X%02X", temps[index + 3], temps[index + 4]);
				AnchorType anchorType = new AnchorType(strAnchorId, temps[index + 2]);
				// 调用回调函数通知
				if (null != iReportCallBack) {
					iReportCallBack.callBack(anchorType);
				}
				return true;
			}
		}
		return false;
	}
	@Override
	public byte getPacketHeader() {
		return AnalyticalUtil.PLAYVOICE_RESPONSE_HEAD;
	}
	@Override
	public int getPacketSize() {
		return AnalyticalUtil.PLAYVOICE_RESPONSE_SIZE;
	}
}
